package com.hnxxxy.service.impl;

import com.github.pagehelper.PageInfo;
import com.hnxxxy.mapper.AssociationAdministrationMapper;
import com.hnxxxy.pojo.Association;
import com.hnxxxy.pojo.Layui;
import com.hnxxxy.pojo.User;
import com.hnxxxy.service.AssociationAdministrationService;
import com.hnxxxy.system.aop.myLog.MyLog;
import com.hnxxxy.utils.Communal;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class AssociationAdministrationServiceImpl implements AssociationAdministrationService {
    private static final Logger log = LogManager.getLogger(AssociationAdministrationServiceImpl.class);

    @Autowired
    AssociationAdministrationMapper associationAdministrationMapper;

    @Autowired
    Communal communal;

    /**
     * 删除本社团的人员
     *
     * @param id user_ass表id
     * @return 操作成功或失败的信息
     */
    @Override
    @MyLog(value = "删除本社团的人员")
    public String deleteAssPeo(int id) {
        log.debug("执行AssociationAdministrationServiceImpl的deleteAssPeo");
        return communal.RUD(associationAdministrationMapper.deleteAssPeo(id));
    }

    /**
     * 删除社团
     *
     * @param a_id 社团a_id
     * @return 操作成功或失败的信息
     */
    @Override
    @MyLog(value = "删除社团")
    public String deleteAssInfo(int a_id) {
        log.debug("执行AssociationAdministrationServiceImpl的deleteAssInfo");
        return communal.RUD(associationAdministrationMapper.deleteAssInfo(a_id));
    }

    /**
     * 社团信息修改
     *
     * @param association 社团信息:社团p_id,社团名字p_name,社团社长username,社团图片a_url
     * @return 操作成功或失败的信息
     */
    @Override
    @MyLog(value = "社团信息修改")
    public String updateAssInfo(Association association, HttpServletRequest request) {
        log.debug("执行AssociationAdministrationServiceImpl的updateAssInfo");
        String usrename = communal.getUser();
        String path1 = request.getSession().getServletContext().getRealPath("/");
        String path2 = StringUtils.substringBefore(path1, "target") + "\\static\\images";

        if (communal.deleteFile(path2.concat("\\").concat(associationAdministrationMapper.queryAssUrl(usrename)))) {
            return communal.RUD(associationAdministrationMapper.updateAssInfo(association));
        } else {
            return communal.resultMap(500, "修改图片失败");
        }

    }

    /**
     * 查询本社团的人员信息
     *
     * @param name 用户姓名
     * @return 查询本社团的人员信息
     */
    @Override
    public String queryAssPeo(String name,int page, int limit) {
        log.debug("执行AssociationAdministrationServiceImpl的queryAssPeo");
        return communal.runterListJson(page,limit,associationAdministrationMapper.queryAssPeo(name));
    }

    /**
     * 条件查询社团信息
     *
     * @param a_name 社团名字
     * @return 条件查询的社团信息
     */
    @Override
    @MyLog(value = "条件查询社团信息")
    public String queryAssInfo(String a_name,int page,int limit) {
        log.debug("执行AssociationAdministrationServiceImpl的queryAssInfo");
        return communal.runterListJson(page,limit,associationAdministrationMapper.queryAssInfo(a_name));
    }

    /**
     * 条件查询社团社长信息
     *
     * @param a_name 社团名字
     * @return 条件查询社团的社长信息
     */
    @Override
    @MyLog(value = "条件查询社团社长信息")
    public String queryAssPre(String a_name,int page,int limit) {
        log.debug("执行AssociationAdministrationServiceImpl的queryAssPre");
        return communal.runterListJson(page,limit,associationAdministrationMapper.queryAssPre(a_name));
    }

    /**
     * 查询所有社团社长信息
     *
     * @return 所有社团社长信息
     */
    @Override
    public String findAllAssPre(int page,int limit) {
        log.debug("执行AssociationAdministrationServiceImpl的findAllAssPre");
        return communal.runterListJson(page,limit,associationAdministrationMapper.findAllAssPre());
    }

    /**
     * 查询所有社团
     *
     * @return 所有社团信息
     */
    @Override
    public String findAllAssInfo(int page, int limit) {
        log.debug("执行AssociationAdministrationServiceImpl的findAllAssInfo");
        return communal.runterListJson(page,limit,associationAdministrationMapper.findAllAssInfo());
    }


    /**
     * 查询本社团所有的人员信息
     *
     * @return 本社团所有的人员信息
     */
    @Override
    public String findAllAssPeo(int page,int limit) {
        log.debug("执行AssociationAdministrationServiceImpl的findAllAssPeo");
        int a_id = associationAdministrationMapper.queryAssUserId(communal.getUser());
        return communal.runterListJson(page,limit,associationAdministrationMapper.findAllAssPeo(a_id));
    }

    /**
     * 查询用户加入的社团信息
     *
     * @return 用户加入的社团信息
     */
    @Override
    public String findAllUserAss(int page,int limit) {
        log.debug("执行AssociationAdministrationServiceImpl的findAllUserAss");
        return communal.runterListJson(page,limit,associationAdministrationMapper.findAllUserAss(communal.getUser()));
    }
}
